<?php
/**
 * Created by PhpStorm.
 * User: kaycn
 * Date: 18-9-18
 * Time: 下午9:30
 *
 * 节约资源 采用单例模式
 *
 */

namespace Pay;

class Js
{
    public $i;

    public static $js;

    public static $driver;

    private function __construct($driver)
    {
        self::$driver = $driver;


    }

    public static function getJs($driver){

    if(self::$js instanceof Js){
        return self::$js;
    }else{
         self::$js = new self($driver);

         return self::$js;
    }
}




//获取今天的账单
    public function day(){

        $js = <<<js
            var a = document.getElementsByClassName("ant-table-row");
           
            var pay = [];
            
            var price =  a[0].childNodes[4].innerText;
               
            var goods = a[0].childNodes[1].innerText;
            
            var time =  a[0].childNodes[0].innerText;

            var arr = {"time":time,"price":price,"goods":goods}
         
            return JSON.stringify(arr);
            
js;
        self::$driver->executeScript($js);

    }

//昨天的账单
    public function yester(){
        $js1 = <<<js
        
        document.getElementsByClassName('quickTimeItem___r5kmW')[1].click();

js;
        self::$driver->executeScript($js1);

    }

//七天的账单

    public function week(){
        $js2 = <<<js
        
        document.getElementsByClassName('quickTimeItem___r5kmW')[2].click();

js;
        self::$driver->executeScript($js2);

    }
    //一个月

    public function mooth(){
        $js1 = <<<js
        
        document.getElementsByClassName('quickTimeItem___r5kmW')[3].click();

js;
        self::$driver->executeScript($js1);

    }

    //最多截取前10条数据

    public function length(){
        $js = <<<js
               var length = document.getElementsByClassName("ant-table-row").length;
                return length;
js;
        return self::$driver->executeScript($js);

    }

    public function get($i)
    {
        $js = <<<js
          var a = document.getElementsByClassName("ant-table-row");

            var price =  a[{$i}].childNodes[4].innerText;
            
            var time =  a[{$i}].childNodes[0].innerText;

               
            var goods = a[{$i}].childNodes[1].innerText;

            var arr = {"time":time,"price":price,"goods":goods};
         
            return JSON.stringify(arr);
js;

        return self::$driver->executeScript($js);



    }

    public function getall(){

    $js = <<<js
        var a = document.getElementsByClassName("ant-table-row");
         var arr=[];
        for(var i =0;i<a.length/2;i++){
            
           
            
             var price =  a[i].childNodes[4].innerText;
            
            var time =  a[i].childNodes[0].innerText;

               
            var goods = a[i].childNodes[1].innerText;

            arr[i] = {"time":time,"price":price,"goods":goods}; 
            
        }
        return JSON.stringify(arr);

js;

    return self::$driver->executeScript($js);




    }

    public function getcount_all($menth){


        self::$driver->get("https://mbillexprod.alipay.com/enterprise/tradeListQuery.htm");

        if(self::$driver->getTitle()=="登录 - 支付宝"){
            $this -> __init();
        }

        $Js = self::getJs(self::$driver);

        $Js->$menth();

        sleep(1);

        return $Js->getall();




    }

}